// pages/user/shop-apply.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      name: '',
      phone: '',
      email: '',
      region: ['', '', ''],
      domain: '',
      experience: '',
      introduction: '',
      courseDirection: '',
      targetStudents: '',
      courseSyllabus: '',
      socialMedia: '',
      referrer: '',
      additionalInfo: ''
    },
    domainOptions: [
      '编程与开发',
      '设计与创意',
      '商业与营销',
      '职业发展',
      '语言学习',
      '健康与生活',
      '兴趣爱好',
      '学术教育',
      '其他'
    ],
    experienceOptions: [
      '无经验',
      '1年以下',
      '1-3年',
      '3-5年',
      '5-10年',
      '10年以上'
    ],
    // 新增数据
    rules: {
      name: [
        { required: true, message: '请输入姓名' }
      ],
      phone: [
        { required: true, message: '请输入手机号' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }
      ],
      email: [
        { required: true, message: '请输入邮箱' },
        { pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/, message: '请输入正确的邮箱' }
      ],
      region: [
        { required: true, message: '请选择所在地区' }
      ],
      domain: [
        { required: true, message: '请选择擅长领域' }
      ],
      experience: [
        { required: true, message: '请选择教学经验' }
      ],
      introduction: [
        { required: true, message: '请填写个人介绍' },
        { min: 50, message: '个人介绍不少于50字' }
      ],
      courseDirection: [
        { required: true, message: '请填写课程方向' }
      ],
      targetStudents: [
        { required: true, message: '请填写目标学员' }
      ]
    },
    isSubmitting: false,
    showSuccessModal: false,
    agreeTerms: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 检查用户是否已同意讲师协议
    const agreementAccepted = wx.getStorageSync('teacherAgreementAccepted');
    
    if (!agreementAccepted) {
      // 如果用户未同意协议，跳转到协议页面
    //   wx.redirectTo({
    //     url: '/pages/user/teacher-agreement'
    //   });
    //   return;
    }
    
    // 如果用户已登录，自动填充部分信息
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      const formData = this.data.formData;
      formData.name = userInfo.realName || '';
      formData.phone = userInfo.phone || '';
      
      this.setData({ formData });
    }
  },

  /**
   * 输入框内容变化处理
   */
  onInputChange(e) {
    const { field } = e.currentTarget.dataset;
    const { value } = e.detail;
    
    this.setData({
      [`formData.${field}`]: value
    });
  },

  /**
   * 选择器变化处理
   */
  onPickerChange(e) {
    const { field } = e.currentTarget.dataset;
    const { value } = e.detail;
    
    this.setData({
      [`formData.${field}`]: value
    });
  },

  /**
   * 地区选择器变化处理
   */
  onRegionChange(e) {
    const { value } = e.detail;
    
    this.setData({
      'formData.region': value
    });
  },

  /**
   * 切换同意条款状态
   */
  toggleAgreeTerms() {
    this.setData({
      agreeTerms: !this.data.agreeTerms
    });
  },

  /**
   * 查看用户协议
   */
  viewAgreement() {
    wx.navigateTo({
      url: '/pages/user/user-agreement'
    });
  },

  /**
   * 表单验证
   */
  validateForm() {
    const { formData, rules } = this.data;
    let isValid = true;
    let errorMessage = '';
    
    // 遍历规则进行验证
    for (const field in rules) {
      const fieldRules = rules[field];
      const value = formData[field];
      
      for (const rule of fieldRules) {
        // 必填验证
        if (rule.required && (!value || (Array.isArray(value) && value.join('') === ''))) {
          isValid = false;
          errorMessage = rule.message;
          break;
        }
        
        // 正则验证
        if (rule.pattern && !rule.pattern.test(value)) {
          isValid = false;
          errorMessage = rule.message;
          break;
        }
        
        // 最小长度验证
        if (rule.min && value.length < rule.min) {
          isValid = false;
          errorMessage = rule.message;
          break;
        }
      }
      
      if (!isValid) {
        wx.showToast({
          title: errorMessage,
          icon: 'none'
        });
        break;
      }
    }
    
    // 验证是否同意条款
    if (isValid && !this.data.agreeTerms) {
      isValid = false;
      wx.showToast({
        title: '请阅读并同意用户协议',
        icon: 'none'
      });
    }
    
    return isValid;
  },

  /**
   * 提交表单
   */
  submitForm() {
    // 表单验证
    if (!this.validateForm()) {
      return;
    }
    
    // 防止重复提交
    if (this.data.isSubmitting) {
      return;
    }
    
    this.setData({
      isSubmitting: true
    });
    
    wx.showLoading({
      title: '提交中...',
      mask: true
    });
    
    // 模拟提交请求
    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        isSubmitting: false,
        showSuccessModal: true
      });
      
      // 打印表单数据（实际开发中应该发送到服务器）
      console.log('提交的表单数据：', this.data.formData);
    }, 1500);
  },

  /**
   * 关闭成功弹窗
   */
  closeSuccessModal() {
    this.setData({
      showSuccessModal: false
    });
    
    // 返回上一页
    wx.navigateBack();
  }
})